package com.danco.training.zaneuskaya.repository.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.danco.training.zaneuskaya.domain.City;
import com.danco.training.zaneuskaya.domain.Country;
import com.danco.training.zaneuskaya.domain.Event;
import com.danco.training.zaneuskaya.repository.EventDao;
/**
 * @author Zaneuskaya Aliaksandra
 *
 */
@Repository
public class EventDaoImpl extends AbstractHibernateDao<Event, Integer>
		implements EventDao {

	private static final String EVENT_CITY = "eventCity";
	private static final String EVENT_COUNTRY = "eventCountry";
	private static final String EVENT_END_DATE = "eventEndDate";
	private static final String EVENT_TITLE = "eventTitle";
	private static final String EVENT_START_DATE = "eventStartDate";

	@Override
	public Event read(Integer PK) throws HibernateException {
		return super.read(Event.class, PK);
	}

	@Override
	public List<Event> readAll() throws HibernateException {
		return super.readAll(Event.class, EVENT_START_DATE);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Event> findEventsByTitle(String title)
			throws HibernateException {
		List<Event> events = new ArrayList<Event>();
		Criteria criteria = getSession().createCriteria(Event.class);
		criteria.add(Restrictions.like(EVENT_TITLE, title));

		events =  modifyCriteria(criteria, EVENT_TITLE).list();
		return events;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Event> findEventsByPeriod(Date from, Date to)
			throws HibernateException {
		List<Event> events = new ArrayList<Event>();
		Criteria criteria = getSession().createCriteria(Event.class);
		criteria.add(Restrictions.between(EVENT_START_DATE, from, to));
		criteria.add(Restrictions.between(EVENT_END_DATE, from, to));
		events =  modifyCriteria(criteria, EVENT_TITLE).list();
		return events;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Event> findEventsByCountry(Country country)
			throws HibernateException {
		List<Event> events = new ArrayList<Event>();
		Criteria criteria = getSession().createCriteria(Event.class);
		criteria.add(Restrictions.eq(EVENT_COUNTRY, country));

		events = modifyCriteria(criteria, EVENT_START_DATE).list();
		return events;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Event> findEventsByCity(City city) throws HibernateException {
		List<Event> events = new ArrayList<Event>();
		Criteria criteria = getSession().createCriteria(Event.class);
		criteria.add(Restrictions.eq(EVENT_CITY, city));
		events = modifyCriteria(criteria, EVENT_START_DATE).list();
		return events;
		
	}

	@SuppressWarnings("unchecked")
	public List<Event> sortEventsByStartDateColumn(String column)
			throws HibernateException {
		List<Event> events = new ArrayList<Event>();
		Criteria criteria = getSession().createCriteria(Event.class);
		criteria.addOrder(Order.desc(column));
		events =  modifyCriteria(criteria, EVENT_TITLE).list();
		return events;
	}

}
